<?php
ini_set('display_errors', 1);
// simple authorization

require 'www_include.php';

// check if user is authorized
$auth = new WwwAuth();
$user_id = $auth->getAuthMember();
$current_user = false;
$md5_secret = '';
if ($user_id) {
    $current_user = $user_id['id'];
    $md5_secret = md5(www_conf::$api_id . '_' . $current_user . '_' . www_conf::$api_secret);
	$social = 1;
}

if(isset($_GET['logout']) && !isset($_POST['first_name']))
{
	setcookie('s_c', '', time());
	setcookie('s_h', '', time());
	unset($_POST);
}
else
if(isset($_COOKIE['s_c']))
{
	$md5_secret = md5(www_conf::$api_id . '_' . $_COOKIE['s_c'] . '_' . www_conf::$api_secret);
	if($md5_secret == $_COOKIE['s_h'])
	{
		$current_user = $_COOKIE['s_c'];
		$md5_secret = $_COOKIE['s_h'];
		$user_id = $current_user;
		$social = 2;
		include_once  'server/include.php';
		$query = 'SELECT * FROM `user_site` WHERE `id`='.(int)$current_user;
		$row = _database::torow($query);
		$name = $row['nick'];

	}
}



if(isset($_POST['first_name']))
{
	include_once 'server/include.php';
	$name = mysql_escape_string($_POST['first_name']);
	if(!trim($name)) $name ='lazy';
	$query = 'INSERT INTO `user_site` SET email=\'\', `nick`=\''.$name.'\'';
	_database::query($query);
	$user_id =  mysql_insert_id();
	$social = 2;
	$current_user = $user_id;
	$md5_secret = md5(www_conf::$api_id . '_' . $current_user . '_' . www_conf::$api_secret);
	setcookie('s_c',$user_id);
	setcookie('s_h',$md5_secret);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <link xmlns="" rel="stylesheet" href="/www/css/css.css"/>
        <title xmlns="">ВЕСЫ</title>
        <script src="http://vkontakte.ru/js/api/openapi.js" type="text/javascript"></script>
        <script src="/www/js/vk.js" type="text/javascript"></script>
        <script src="/www/js/swfobject.js" type="text/javascript"></script>
    </head>
    <body>
        <script type="text/javascript">
            VK.init({apiId: <?= www_conf::$api_id ?>,nameTransportPath: '/www/xd_receiver.html'});
        </script>
        <div class="header">
            <div class="header_txt">Весы</div>
        </div>
        <? if (!$current_user) {
 ?>
            <div class="not_logged_outer">
                <div class="not_logged">
                    <script type="text/javascript">
                        var ra = checkLoginStatus(0);
                    </script>
                    <div class="vk_login">
                        <div class="txt_h">Войти, используя аккаунт Вконтакте:<div class="help"><a onfocus="this.blur()" alt="помощь при авторизации" title="помощь при авторизации" href="/www/help#vklogin"></a></div></div>
                        <div class="clr"></div>
                        <div class="vk_i_container">
                            <a alt="Кликните, чтобы войти" title="Кликните, чтобы войти" onfocus="this.blur()" href="javascript:void(0)" onclick="javascript:login()"></a>
                        </div>
                    </div>
					<div class="anon">
						<p>анонимный вход</p>
						<form method="post">
							<p>Введите имя:</p>
							<input name="first_name"></input>
							<input type="submit" value="войти"></input>
						</form>
					</div>
                    <!--div class="norm_login">
                        <div class="txt_h">Войти, используя E-mail:<div class="help"><a onfocus="this.blur()" alt="помощь при авторизации" title="помощь при авторизации" href="/www/help#login"></a></div></div>
                        <div class="clr"></div>
                        <form method="post">
                            <div class="field">
                                E-mail:
                            </div>
                            <div class="field_in">
                                <input name="email"></input>
                            </div>
                            <div class="field">
                                Пароль:
                            </div>
                            <div class="field_in">
                                <input name="pass"></input>
                            </div>
                            <div class="btnsubmitr">
                                <input type="submit" value="Зарегистрироваться" />
                            </div>
                            <div class="btnsubmitl">
                                <input type="submit" value="Войти" />
                            </div>
                        </form>
                    </div-->
                </div>
            </div>
<? } ?>

<? if ($current_user) { ?>
            <!--div class="user_logged_in">User <?= $current_user ?> Logged In</div-->
            <div class="logout">
				<?if($social == 1){?>
                <a href="javascript:void(0)" title="logout" onclick="javascript:logout()">выйти</a>
				<?}if($social == 2){?>
				<a href="javascript:void(0)" title="logout" onclick="javascript:logout_s()">выйти</a>
				<?}?>
            </div>
            <script type="text/javascript">
                var hash = "<?= $md5_secret ?>"
                var SWF_WIDTH = "<?= www_conf::$swf_width ?>"
                var SWF_HEIGHT = "<?= www_conf::$swf_height ?>"
                var SWF_URL = "<?= www_conf::$swf_url ?>"
				<?if($social == 1){?>
                checkLoginStatus(1,vk_getUserInfo);
				<?}?>
				<?if($social == 2){?>
                var social = {photo:null,sex:0,first_name:"<?=$name?>",last_name:"",uid:<?=$current_user?>}
				social.social = 2;
				draw_flash(social,<?=$current_user?>);
				<?}?>
            </script>
            <div id="flashPreContainer">
                <div id="flashContainer" class="flashContainer">
                    please, update your flash player
                </div>
            </div>
<? } ?>

    </body>
</html>


